package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;

import javax.security.auth.kerberos.DelegationPermission;
import java.util.List;

@Service
public class SetmealServiceImpl  implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
        //
        Setmeal setmeal = new Setmeal();
        //将套餐数据传递给新建的套餐表
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入数据到表
        setmealMapper.insert(setmeal);

        //获取套餐对应的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null) {
            //设置id
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insertBath(setmealDishes);
        }
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //
      Page<SetmealVO> pa= setmealMapper.pageQuery(setmealPageQueryDTO);

      //设置分页返回结果
        PageResult pageResult = new PageResult();
        //返回总数和当前记录数
        pageResult.setTotal(pa.getTotal());
        pageResult.setRecords(pa.getResult());


        return pageResult;
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //启售状态套餐
      int count=  setmealMapper.getSetmealStatus(ids, StatusConstant.ENABLE);

      if (count >0 ){
         throw  new  DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
      }
      setmealMapper.delete(ids);
        for (Long setmealId : ids) {
            setmealDishMapper.deleteBySetmealId(setmealId);
        }
    }

    /**
     * 查询套餐id修改套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
       SetmealVO setmealVO= setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //将套餐数据传递给新建的套餐表
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1、修改套餐表，执行update，
        setmealMapper.update(setmeal);



        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            // 删除关联菜品信息，套餐id
            setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
            // 添加新的关联菜品
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 套餐启售停售
     * @param status
     * @param id
     */
    @Override
    public void getStatus(Integer status, Long id) {
        if (status.equals(StatusConstant.ENABLE)) {

            // 查询套餐对应的菜品是否包含停售，如果包含则启动失败
            int count = setmealDishMapper.getDishByStatusAndSetmealId(StatusConstant.DISABLE, id);
            if (count > 0) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.updateById(setmeal);
        }
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }






}

